package com.ihrm.modules.sys.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ihrm.common.exception.RRException;
import com.ihrm.common.utils.IdWorker;
import com.ihrm.common.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import com.ihrm.modules.sys.entity.UserEntity;
import com.ihrm.modules.sys.service.UserService;
import com.ihrm.common.utils.PageUtils;
import com.ihrm.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 * @author gaogzhen
 * @email gaogzhen@foxmail.com
 * @date 2020-12-17 20:11:23
 */
@RestController
@RequestMapping("sys/user")
@Api(tags = "User")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private IdWorker idWorker;

    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("列表")
    @RequiresPermissions("sys:user:list")
    public R list(@RequestParam Map<String, Object> params) {

        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询")
    @RequiresPermissions("sys:user:info")
    public R info(@PathVariable("id") Long id) {
        UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("添加")
    @RequiresPermissions("sys:user:save")
    public R save(@RequestBody UserEntity user) {
        userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    @RequiresPermissions("sys:user:update")
    public R update(@RequestBody UserEntity user) {
        // ValidatorUtils.validateEntity(user);
        userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation("删除")
    @RequiresPermissions("sys:user:delete")
    public R delete(@RequestBody Long[] ids) {
        userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
//
//    @PostMapping("/assignRoles")
//    @ApiOperation("分配角色")
//    @RequiresPermissions("sys:user:assignRoles")
//    public R assignRoles(@RequestBody Map<String, Object> map) {
//        if (ObjectUtils.isEmpty(map) || !map.containsKey("id") || !map.containsKey("roleIds")) {
//            throw new RRException("用户或者角色不能为空");
//        }
//        userService.assignRoles(map);
//        return R.ok();
//    }
//
//    @PostMapping("/login")
//    @ApiOperation("登录")
//    public R login(@RequestBody Map<String, Object> map) {
//        if (!map.containsKey("mobile") || !map.containsKey("password")) {
//            throw new RRException("手机号或者密码为空");
//        }
//
//        String token = userService.login(map);
//
//        return R.ok().put("token", token);
//    }

    /**
     * 导入excel，批量添加用户
     * @param file      excel
     * @return          ok
     */
    @ApiOperation("批量导入")
    @PostMapping("/import")
    @RequiresPermissions("sys:user:save")
    public R importFile(@RequestBody MultipartFile file) throws IOException {
        userService.importFile(file);

        return R.ok();
    }

    /**
     * 当月人事报表导出
     * @param month 当月，2021-03
     */
    @ApiOperation("导出")
    @GetMapping("/export/{month}")
    @RequiresPermissions("sys:user:list")
    public void export(@PathVariable String month, HttpServletResponse response) throws IOException {
        userService.export(month, response);

//        return R.ok();
    }

}
